<?php
/**
 * Created by PhpStorm.
 * User: hgq
 * Date: 16/6/22
 * Time: 下午1:23
 */
use yii\helpers\Url;
$this->title = '商品不可见';

$web = Url::base();

?>
<script type="application/javascript">

    var hideGoodsList = new Array();

    $(function ($) {

        var response_institution_code = <?=json_encode($institution_code)?>;
        var response_child_institution_code = <?=json_encode($child_institution_code)?>;
        if (response_institution_code == null || response_institution_code == '') {
            $("#super_institution").html('<option  value="" selected="selected"></option>');
        } else {
            $("#super_institution").html('<option  value=""></option>');
        }
        var data = {};//获取有下级机构的上级机构列表
        var re = requestJson("get-super-institution-handler", data);
        if (re.status == 0) {
            var inst = re.data.superinst;
            if (inst != false) {
                $.each(inst, function (i, item) {
                    if (response_institution_code != null && response_institution_code != '' && item.code == response_institution_code) {
                        $("#super_institution").append("<option  value='" + item.code + "' selected='selected'>" + item.name + "</option>");
                    } else {
                        $("#super_institution").append("<option  value='" + item.code + "'>" + item.name + "</option>");
                    }
                });
            }
        }

        var childinst = getChildInst(response_institution_code);
        if (response_child_institution_code == null || response_child_institution_code == '') {
            $("#child_institution").html('<option  value="" selected="selected"></option>');
        } else {
            $("#child_institution").html('<option  value=""></option>');
        }
        if (childinst != null && childinst != '') {
            $.each(childinst, function (i, item) {
                if (response_child_institution_code != null && response_child_institution_code != '' && item.code == response_child_institution_code) {
                    $("#child_institution").append("<option  value='" + item.code + "' selected='selected'>" + item.name + "</option>");
                } else {
                    $("#child_institution").append("<option  value='" + item.code + "'>" + item.name + "</option>");
                }
            });
        }

        //设置上级机构是否可选择
        var currentManager = <?=json_encode(\app\models\Manager::getCurrentManager()) ?>;
        var current_institution_code = currentManager.institution_code;
        if (current_institution_code != null && current_institution_code != '' && current_institution_code != '0') {
            $("#super_institution").attr("disabled", "disabled");
        }


        $("#super_institution").change(function () {
            var me = $(this);
            $("#child_institution").html('<option value=""></option>');
            var child = getChildInst(me.val());
            if (child != false) {
                $.each(child, function (i, item) {
                    $("#child_institution").append("<option  value='" + item.code + "'>" + item.name + "</option>");
                });
            }
        });

    });

    // 获取上级机构的下级机构列表
    function getChildInst(institution_code) {

        var data = {};
        data.institution_code = institution_code;
        var re = requestJson("get-child-institution-handler", data);
        if (re.status == 0) {
            return re.data.childinst;
        } else {
            return null;
        }

    }

    //筛选
    function filter() {
        var institution_code = '';
        var child_institution_code = '';

        institution_code = $("#super_institution").val();
        child_institution_code = $("#child_institution").val();

        var href = window.location.href;
        href = setUrlParam(href, 'page', 1);
        if (institution_code != '' && institution_code != null) {
            href = setUrlParam(href, 'institution_code', institution_code);
        }
        if (child_institution_code != '' && child_institution_code != null) {
            href = setUrlParam(href, 'child_institution_code', child_institution_code);
        }
        window.location.href = href;
    }

    // 设置按钮
    function add() {
        var super_inst_code = $("#super_institution").val();
        var data = {};
        data.super_inst_code = super_inst_code;

        $("#hideChildEdit").html('');
        requestHtml('hide-child-inst-part-view', data, true, function (responseHtml) {
            $("#hideChildEdit").html(responseHtml);
        }, function () {
            $("#hideChildEdit").html('无数据');
        });
    }

    // 导入按钮点击
    function importClick() {
        $("#import_file_error").html('');
        $("#import_goods_list").html('');
        $("#upload_file").val('');
    }

    //导入文件
    function importFile() {
        $("#import_file_error").html('');
        $("#import_goods_list").html('');
        loading.show();
        hideGoodsList = new Array();
        if ($("#upload_file").val() != '') {
            $("#upload_file_submit_form").submit();
            $ex1 = 0;
            $("#upload_exec_target").load(function () {
                if ($ex1 == 0) {
                    try {
                        var result = eval('(' + ($("#upload_exec_target").contents().find("body").html()) + ')');

                        if (result.status == 0) {

                            hideGoodsList = [];
                            var goods = {};
                            if (result.data != null && result.data != '') {
                                var re = result.data.goods_list;
                                $.each(re, function (i, item) {
                                    goods = {};
                                    goods.super_inst_code = item.super_inst_code;
                                    goods.super_inst_name = item.super_inst_name;
                                    goods.goods_code = item.goods_code;
                                    goods.goods_name = item.goods_name;
                                    goods.child_inst_coed = item.child_inst_coed;
                                    goods.child_inst_name = item.child_inst_name;
                                    goods.is_hide = item.is_hide;
                                    goods.error = item.error;
                                    hideGoodsList[i] = goods;
                                    if (item.error != null && item.error != '') {
                                        $("#import_goods_list").append("<tr><td>" + item.super_inst_name + "</td><td>" + item.goods_code + "</td><td>" + item.goods_name + "</td><td>" + item.child_inst_coed + "</td><td>" + item.child_inst_name + "</td><td>" + item.is_hide + "</td><td><span class=\"label label-danger\" onclick=\"errClick('" + item.error + "')\">ERR</span></td></tr>");
                                    } else {
                                        $("#import_goods_list").append("<tr><td>" + item.super_inst_name + "</td><td>" + item.goods_code + "</td><td>" + item.goods_name + "</td><td>" + item.child_inst_coed + "</td><td>" + item.child_inst_name + "</td><td>" + item.is_hide + "</td><td></td></tr>");
                                    }
                                });
                            }

                        } else {
                            var re = eval('(' + result.message + ')');
                            $("#import_file_error").html(re);
                        }

                    } catch (err) {
                        var re = "网络异常，请重试";
                        $("#import_file_error").html(re + err.message);
                    }

                    loading.hide();
                    $ex1 = 1;
                }
            });
        }
    }

    // 错误信息查看
    function errClick(dcm) {
        $("#err_message").html(dcm);
        $("#errMessageModal").modal('show');
    }

    // 导入确认
    function importConfirm() {
        if (hideGoodsList == null || hideGoodsList == '' || hideGoodsList.length <= 0) {
            $("#err_message").html("请选择需要导入的数据！");
            $("#errMessageModal").modal('show');
        }
        $isExistsErr = 0;
        for (var i = 0; i < hideGoodsList.length; i++) {
            if (hideGoodsList[i].error != null && hideGoodsList[i].error != '') {
                $isExistsErr = 1;
            }
        }
        if ($isExistsErr == 1) {
            $("#err_message").html("选择的表格中存在错误数据，请修改相关内容后重新导入！");
            $("#errMessageModal").modal('show');
        }

        loading.show();
        var data = {};
        data.hideGoodsList = hideGoodsList;
        var re = requestJson("import-hide-child-inst-confirm-handler", data);
        $("#hint_message").text('');
        if (re.status == 0) {
            loading.hide();
            $("#hint_message").text('操作成功！');
        } else {
            loading.hide();
            $("#hint_message").text('操作失败！');
        }
        $("#hintMessageModal").modal('show');

    }

    function hideHintMessageModal() {
        $("#hintMessageModal").modal('hide');
    }

    //设置模态窗 1.当用户点击模态框外部时不会关闭 2.当按下 escape 键时不会关闭 3.当初始化时不显示模态框。
    $(function () {
        $('#myModalHideChildEdit').modal({
            keyboard: false, backdrop: 'static', show: false
        });
        $('#myModalHideChildImport').modal({
            keyboard: false, backdrop: 'static', show: false
        });
    });


</script>
<style type="text/css">
    .modal-body {
        overflow: auto;
        max-height: 600px;
    }
</style>
<!-- head filter-->
<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group">
                <button class="btn btn-primary" type="button" onclick="add()" data-toggle="modal"
                        href="#myModalHideChildEdit"><i class="fa fa-cog"></i> 设置
                </button>
                <button class="btn btn-primary" type="button" onclick="importClick()" data-toggle="modal"
                        href="#myModalHideChildImport"><i class="fa fa-upload"></i> 导入
                </button>
            </div>
            <div class="pull-right">
                <div class="form-group">
                    <label for="exampleInputEmail2">当前机构</label>
                    <select id="super_institution" class="form-control" style=" min-width:140px; max-width: 200px"
                            onchange="">
                    </select>
                </div>
                <div class="form-group">
                    <label for="exampleInputPassword2" style=" margin-left: 5px">旗下机构</label>
                    <select id="child_institution" class="form-control" style=" min-width:140px; max-width: 200px"
                            onchange="">
                    </select>
                </div>
                <button type="button" class="btn btn-primary" onclick="filter()"><i class="fa fa-search"></i> 搜索
                </button>
            </div>
        </div>
    </div>
</div>
<!-- body table-->
<div class="panel panel-default" id="goodsList">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th>商品编码</th>
                    <th>商品名称</th>
                    <th>旗下机构编码</th>
                    <th>旗下机构名称</th>
                </tr>
                </thead>
                <tbody>
                <?php
                for ($i = 0; $i < count($list); $i++) {
                    ?>
                    <tr>
                        <td><?= $list[$i]['goods_code'] ?></td>
                        <td><?= $list[$i]['goods_name'] ?></td>
                        <td><?= $list[$i]['child_institution_code'] ?></td>
                        <td><?= $list[$i]['child_institution_name'] ?></td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>
<!-- Modal hide child edit-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="myModalHideChildEdit" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">对旗下机构不可见设置</h4>
            </div>
            <div id="hideChildEdit" class="modal-body">
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 关闭</button>
            </div>
        </div>
    </div>
</div>
<!-- Modal hide child import-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="myModalHideChildImport" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">对旗下机构不可见设置-导入</h4>
            </div>
            <div id="hideChildImport" class="modal-body">
                <div class="form-inline">
                    <div class="pull-left">
                        <form id="upload_file_submit_form" enctype="multipart/form-data" method="post"
                              action="import-hide-child-inst" target="upload_exec_target">
                            <div class="form-group">
                                <input id="upload_file" name="file" type="file" onchange="importFile();"/>
                            </div>
                            <div class="form-group">
                                <a id="excel-download" href="<?= $web ?>/templet/不可见产品导入模板.xlsx">
                                    下载excel模板
                                </a>
                            </div>
                        </form>
                        <iframe id="upload_exec_target" name="upload_exec_target" style="display: none;"></iframe>
                    </div>
                </div>
                <div id="import_file_error"></div>
                <!-- body table-->
                <div id="body_table">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover table-condensed" style=" margin-top: 5px">
                            <thead class="nowrap">
                            <tr>
                                <th style="width:15%">上级机构名称</th>
                                <th style="width:20%">商品编码</th>
                                <th style="width:30%">商品名称</th>
                                <th style="width:10%">旗下机构编码</th>
                                <th style="width:15%">旗下机构名称</th>
                                <th style="width:9%">是否可见</th>
                                <th style="width:1%">
                                    <span class="popovers" data-trigger="hover"
                                          data-placement="left" data-original-title="提示列"
                                          data-content="当导入的表格数据有错误时会出现ERR标记，请点击ERR查看错误提示"><i
                                            class="fa fa-question"></i></span>
                                </th>
                            </tr>
                            </thead>
                            <tbody id="import_goods_list">

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="importConfirm()"><i
                        class="fa fa-ok"></i> 确认导入
                </button>
                <button class="btn btn-primary" data-dismiss="modal"><i class="fa fa-remove"></i> 关闭</button>
            </div>
        </div>
    </div>
</div>

<!--提示对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="hintMessageModal" class="modal fade">
    <div class="modal-dialog modal-sms">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">消息</h4>
            </div>
            <div class="modal-body" id="hint_message" align="center">
            </div>
        </div>
    </div>
</div>

<!--错误对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="errMessageModal" class="modal fade">
    <div class="modal-dialog modal-sms">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">错误</h4>
            </div>
            <div class="modal-body" id="err_message">
            </div>
        </div>
    </div>
</div>